using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace ProtocToCSharp
{
    class Program
    {
        static bool IsTest = false;
        static string[] TestParam = new string[]{
           @"E:\svn\XLSVN\Code\Solutions\ProtocToCSharp\bin\Debug\netcoreapp3.1\ProtocToCSharp.exe",
           @"--root_path=E:\svn\XLSVN\Code\Solutions\ProtocToCSharp\bin\Debug\netcoreapp3.1\Protos",
           @"--build_exe=E:\svn\XLSVN\Code\Solutions\ProtocToCSharp\bin\Debug\netcoreapp3.1\protoc.exe",
           @"--out_path=E:\svn\XLSVN\Code\Solutions\ProtocToCSharp\bin\Debug\netcoreapp3.1\Out"
        };
        static void Main(string[] args)
        {


            if (IsTest)
            {
                args = TestParam;
            }
           




            Console.WriteLine("*********************************");
            Console.WriteLine("*                               *");
            Console.WriteLine("*         ProtocToCSharp        *");
            Console.WriteLine("*                               *");
            Console.WriteLine("*********************************");

            Console.WriteLine("v0.1");
            Console.WriteLine("--root_path= 设置转换的根目录，默认为exe所在目录");
            Console.WriteLine("--build_exe= 设置protoc的编译程序，默认为protoc.exe(v3.1.x)");
            Console.WriteLine("--out_path= 设置导出文件保存的根目录，默认为根目录");
            Console.WriteLine("--run_exit= 执行完成后是否关闭工具");
            Console.WriteLine("--debug= 是否为debug模式");


            ArgModel ArgModel = new ArgModel(args);
            ArgModel.Debug($"RootPath  :  {ArgModel.RootPath}");
            ArgModel.Debug($"BuildertPath  :  {ArgModel.BuildertPath}");
            ArgModel.Debug($"OutPath  :  {ArgModel.OutPath}");
            ArgModel.Debug($"IsDebug  :  {ArgModel.IsDebug}");
            ArgModel.Debug($"AutoExit  :  {ArgModel.AutoExit}");


            if (!ArgModel.Check())
            {

                return;
            }




           //DirectoryInfo root = ArgModel.RootPath;// new DirectoryInfo(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
           //FileInfo builder = ArgModel.BuildertPath;//new FileInfo(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "protoc.exe");
           //DirectoryInfo outpath = ArgModel.OutPath;// null;
           //bool AutoExit = ArgModel.AutoExit;
         
            List<(string, string)> protocFiles = new List<(string, string)>();

            ArgModel.RootPath.GetAllFile("*.proto").ForEach(f =>
            {
                //来源相对路径
                string _tarDir = "";
                string _sourceDir = f.FullName.Replace(ArgModel.RootPath.FullName, "");
                _sourceDir = _sourceDir.Substring(0, _sourceDir.Length - f.Name.Length -1 ).Replace("\\", "/");
                _tarDir = (ArgModel.OutPath.FullName + _sourceDir).Replace("\\", "/");

                if (string.IsNullOrEmpty(_sourceDir))
                {
                    _sourceDir = f.Name;
                }
                else
                {

                    _sourceDir = _sourceDir.Substring(1, _sourceDir.Length-1) +  $"/{f.Name}";
                }

        
                //目标目录

                protocFiles.Add((
                    $"\"{_sourceDir}\"",
                    $"\"{_tarDir}\""
                ));

                DirectoryInfo dir = new DirectoryInfo(_tarDir);
                dir.Create();
            });

            //e:
            string cmd = $"/c  {ArgModel.RootPath.FullName.Substring(0, ArgModel.RootPath.FullName.IndexOf(":") + 1)} ";
            //cd e:/.../...
            cmd += $"& cd \"{ArgModel.RootPath.FullName}\"";
            // Console.WriteLine(cmd);

            ArgModel.Debug(cmd);
            protocFiles.ForEach(p =>
            {
                string _cmd = $"& \"{ArgModel.BuildertPath}\" ";
                _cmd += $" --proto_path=./ {p.Item1}";
                _cmd += $" --csharp_out={p.Item2}";
                cmd += _cmd;
                //Console.WriteLine(_cmd);
                ArgModel.Debug(_cmd);


            });

            using (Process myPro = new Process())
            {
                //指定启动进程是调用的应用程序和命令行参数                    
                myPro.StartInfo = new ProcessStartInfo("cmd.exe", cmd );
                myPro.Start();
                myPro.WaitForExit();
                myPro.Close();
                myPro.Dispose();

            }

            if (!ArgModel.AutoExit)
            {
                Console.WriteLine("..输入任意键结束");
                Console.ReadKey();

            }
        }
    }


    
}
